<?php
	include_once 'site_config.php';
	include_once 'ra_global.php';
	$id='shell';
	$wxlist=array(
			"shell"		=>array(APPID,APPSECRET),
	);
	$appid = $wxlist[$id][0];
	$appsecret = $wxlist[$id][1];
	$mem = new Memcache;
	$mem->connect('localhost', 11211) or die ("Could not connect");
	$access_token = $mem->get($id);
	if (empty($access_token)){
		$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
		$res = http_request($url);
		$result = json_decode($res, true);
		$access_token = $result["access_token"];
		$mem->set($id, $access_token, 0, 7000);
	}
	/*var_dump($access_token);
	$url='https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token='.$access_token;
	$date='{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": 123}}}';
	$save_dir='logo/';
	getImage($url);
	$r=json_decode(getticket($date,$url));
	var_dump($r);*/
	foreach($db->ig2_select('mae_shop','tid<=90010') as $shop){
		$url='https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token='.$access_token;
		$filename=$shop['tid'].'.jpg';
		$date='{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": '.$shop['tid'].'}}}';
        $dirname = "shopcode/";
		$r=json_decode(getticket($date,$url));
		$url='https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket='.$r->ticket;
		var_dump(getCode($url,$filename,$dirname));
		$url='https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token='.$access_token;
		$filename=$shop['tid'].'.jpg';
		$date='{"action_name": "QR_LIMIT_STR_SCENE", "action_info": {"scene": {"scene_str": '.$shop['code'].'}}}';
        $dirname = "repaircode/";
		$r=json_decode(getticket($date,$url));
		$url='https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket='.$r->ticket;
		var_dump(getCode($url,$filename,$dirname));
	}
	function getticket($data,$url){
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
		curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		$tmpInfo = curl_exec($ch);
		if (curl_errno($ch)) {
		  return curl_error($ch);
		}
		curl_close($ch);
		return $tmpInfo;
	}
	function http_request($url, $data = null){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }
	function getImage($url,$filename){  
		 $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_HEADER, 0);    
        curl_setopt($ch, CURLOPT_NOBODY, 0);    //对body进行输出。
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, TRUE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);
        $package = curl_exec($ch);
        $httpinfo = curl_getinfo($ch);
       
        curl_close($ch);
        $media = array_merge(array('mediaBody' => $package), $httpinfo);
        //求出文件格式
        preg_match('/\w\/(\w+)/i', $media["content_type"], $extmatches);
        $fileExt = $extmatches[1];
        //$filename = time().rand(100,999).".{$fileExt}";
        $dirname = "shopcode/";
        if(!file_exists($dirname)){
            mkdir($dirname,0777,true);
        }
        file_put_contents($dirname.$filename,$media['mediaBody']);
        return $dirname.$filename;
	} 
	function getCode($url,$filename,$dirname){  
		 $ch = curl_init($url);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
		curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $package = curl_exec($ch);
        $httpinfo = curl_getinfo($ch);
       
        curl_close($ch);
        $media = array_merge(array('mediaBody' => $package), $httpinfo);
        if(!file_exists($dirname)){
            mkdir($dirname,0777,true);
        }
        file_put_contents($dirname.$filename,$media['mediaBody']);
        return $dirname.$filename;
	}   
	function api_notice_increment($url, $data){
        $ch = curl_init();
        $header = "Accept-Charset: utf-8";
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $tmpInfo = curl_exec($ch);
        if (curl_errno($ch)) {
            //curl_close( $ch )
            return $ch;
        }else{
            //curl_close( $ch ) 
            return $tmpInfo;
        }
        curl_close( $ch ) ;
    }
	function saveMedia($url){
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_HEADER, 0);    
        curl_setopt($ch, CURLOPT_NOBODY, 0);    //对body进行输出。
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $package = curl_exec($ch);
        $httpinfo = curl_getinfo($ch);
		var_dump($httpinfo);
		var_dump($package);
    }
?>